package com.seio.edc.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
	/** 缺省日期格式 */
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

	/** 缺省时间格式 */
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

	/** 缺省月格式 */
	public static final String DEFAULT_MONTH = "MONTH";

	/** 缺省年格式 */
	public static final String DEFAULT_YEAR = "YEAR";

	/** 缺省日格式 */
	public static final String DEFAULT_DAY = "DAY";

	/** 缺省小时格式 */
	public static final String DEFAULT_HOUR = "HOUR";

	/** 缺省分钟格式 */
	public static final String DEFAULT_MINUTE = "MINUTE";

	/** 缺省秒钟格式 */
	public static final String DEFAULT_SECOND = "SECOND";

	/** 缺省长日期格式 */
	public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm";

	/** 缺省长日期格式,精确到秒 */
	public static final String DEFAULT_DATETIME_FORMAT_SEC = "yyyy-MM-dd HH:mm:ss";

	/** 缺省长日期格式（年、月） */
	public static final String DEFAULT_DATE_FORMAT_YM = "yyyy-MM";

	/** 缺省中文日期格式 */
	public static final String DEFAULT_DATE_zh_CN_FORMAT = "yyyy年MM月dd日";

	/** 缺省中文日期（年、月）格式 */
	public static final String DEFAULT_DATE_zh_CN_FORMAT_YM = "yyyy年MM月";

	/** 缺省日期短格式 */
	public static final String DEFAULT_DATE_SHORT_FORMAT = "yyMMdd";

	/**
	 * 获取当前日期
	 * 
	 * @return 当前日期的字符串,如2002-12-12
	 */
	public static String today() {
		SimpleDateFormat dateformat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		java.util.Date currentTime = new java.util.Date();
		String dateString = dateformat.format(currentTime);
		return dateString;
	}

	/**
	 * 按指定格式获取当前日期
	 * 
	 * @param strFormat
	 *            ,日期返回格式,如'yyyy-MM-dd'
	 * @return 当前日期的字符串,如2002-12-12
	 */
	public static String today(String strFormat) {
		SimpleDateFormat dateformat = new SimpleDateFormat(strFormat);
		java.util.Date currentTime = new java.util.Date();
		String dateString = dateformat.format(currentTime);
		return dateString;
	}

	/**
	 * 获取当前时间
	 * 
	 * @return 当前时间,如:21:10:12
	 */
	public static String time() {
		SimpleDateFormat dateformat = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
		java.util.Date currentTime = new java.util.Date();
		String dateString = dateformat.format(currentTime);
		return dateString;
	}

	/**
	 * 按指定格式获取当前时间
	 * 
	 * @param strFormat
	 *            ,日期返回格式,如'HH:mm:ss'
	 * @return 当前时间,如:21:10:12
	 */
	public static String time(String strFormat) {
		SimpleDateFormat dateformat = new SimpleDateFormat(strFormat);
		java.util.Date currentTime = new java.util.Date();
		String dateString = dateformat.format(currentTime);
		return dateString;
	}

	/**
	 * 取得相对于当前时间增加天数/月数/年数后的日期. <br>
	 * 欲取得当前日期5天前的日期,可做如下调用:<br>
	 * getAddDate("DATE", -5).
	 * 
	 * @param field
	 *            ,段,如"year","month","day",对大小写不敏感
	 * @param amount
	 *            ,增加的数量(减少用负数表示),如:5,-1
	 * @return 格式化后的字符串 如"2000-02-01 21:10:12"
	 */
	public static String getAddDate(String field, int amount) {
		// 当前日期和前一天
		SimpleDateFormat dateformat = new SimpleDateFormat(
				DEFAULT_DATETIME_FORMAT_SEC);
		Calendar rightNow = Calendar.getInstance();
		java.util.Date dt = new java.util.Date();
		rightNow.setTime(dt);

		int intField = 0;
		String tmpField = field.toUpperCase();

		intField = Calendar.DATE;
		if (tmpField.trim().equals(DEFAULT_YEAR))
			intField = Calendar.YEAR;
		else if (tmpField.trim().equals(DEFAULT_MONTH))
			intField = Calendar.MONTH;
		else if (tmpField.trim().equals(DEFAULT_DAY))
			intField = Calendar.DAY_OF_MONTH;
		else if (tmpField.trim().equals(DEFAULT_HOUR))
			intField = Calendar.HOUR_OF_DAY;
		else if (tmpField.trim().equals(DEFAULT_MINUTE))
			intField = Calendar.MINUTE;
		else if (tmpField.trim().equals(DEFAULT_SECOND))
			intField = Calendar.SECOND;

		rightNow.add(intField, amount);
		String day = dateformat.format(rightNow.getTime());
		return day;
	}

	/**
	 * 取得相对于当前时间增加天数/月数/年数后的日期,按指定格式输出. 欲取得当前日期5天前的日期,可做如下调用:<br>
	 * getAddDate("DATE", -5, 'yyyy-MM-dd HH:mm:ss').
	 * 
	 * @param field
	 *            ,段,如"year","month","day",对大小写不敏感
	 * @param amount
	 *            ,增加的数量(减少用负数表示),如:5,-1
	 * @param strFormat
	 *            ,日期返回格式,如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return 格式化后的字符串 如"2000-02-01 21:10:12"
	 */
	public static String getAddDate(String field, int amount, String strFormat) {
		// 当前日期和前一天
		SimpleDateFormat dateformat = new SimpleDateFormat(strFormat);
		Calendar rightNow = Calendar.getInstance();
		Date dt = new Date();
		rightNow.setTime(dt);

		int intField = 0;
		String tmpField = field.toUpperCase();

		intField = Calendar.DATE;
		if (tmpField.trim().equals(DEFAULT_YEAR))
			intField = Calendar.YEAR;
		else if (tmpField.trim().equals(DEFAULT_MONTH))
			intField = Calendar.MONTH;
		else if (tmpField.trim().equals(DEFAULT_DAY))
			intField = Calendar.DAY_OF_MONTH;
		else if (tmpField.trim().equals(DEFAULT_HOUR))
			intField = Calendar.HOUR_OF_DAY;
		else if (tmpField.trim().equals(DEFAULT_MINUTE))
			intField = Calendar.MINUTE;
		else if (tmpField.trim().equals(DEFAULT_SECOND))
			intField = Calendar.SECOND;

		rightNow.add(intField, amount);
		String day = dateformat.format(rightNow.getTime());
		return day;
	}

	/**
	 * 获取办理期限，过滤节假日
	 * 
	 * @param days提交的天数
	 * @param flags设定的节假日列表
	 * @return 实际的办理期限
	 */
	public static String getDistDay(int days, String[] flags) {
		int tempday = days;
		int count = 0;
		int len = flags.length;
		for (int i = 0; i < len; i++) {
			if (flags[i].trim().equals("1")) {
				count += 1;
			}
			if (count == days) {
				tempday = i;
				break;
			}
		}
		return getAddDate(DEFAULT_DAY, tempday, DEFAULT_DATE_FORMAT);
	}

	/**
	 * 功能：对于给定的时间增加天数/月数/年数后的日期,按指定格式输出
	 * 
	 * @param date
	 *            ,要改变的日期
	 * @param field
	 *            ,日期改变的字段，YEAR,MONTH,DAY
	 * @param amount
	 *            ,改变量,增加的数量(减少用负数表示),如:5,-1
	 * @param strFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return
	 * @throws ParseException
	 */
	public static String getAddDate(String date, String field, int amount,
			String strFormat) throws ParseException {
		// 当前日期和前一天

		SimpleDateFormat dateformat = new SimpleDateFormat(strFormat);
		Calendar rightNow = Calendar.getInstance();
		Date tempdate = dateformat.parse(date);
		rightNow.setTime(tempdate);

		int intField = 0;
		String tmpField = field.toUpperCase();

		intField = Calendar.DATE;
		if (tmpField.trim().equals(DEFAULT_YEAR))
			intField = Calendar.YEAR;
		else if (tmpField.trim().equals(DEFAULT_MONTH))
			intField = Calendar.MONTH;
		else if (tmpField.trim().equals(DEFAULT_DAY))
			intField = Calendar.DAY_OF_MONTH;
		else if (tmpField.trim().equals(DEFAULT_HOUR))
			intField = Calendar.HOUR_OF_DAY;
		else if (tmpField.trim().equals(DEFAULT_MINUTE))
			intField = Calendar.MINUTE;
		else if (tmpField.trim().equals(DEFAULT_SECOND))
			intField = Calendar.SECOND;

		rightNow.add(intField, amount);
		String day = dateformat.format(rightNow.getTime());
		return day;
	}

	/**
	 * 功能：对于给定的时间增加天数/月数/年数后的日期,按指定格式输出
	 * 
	 * @param date
	 *            ,要改变的日期
	 * @param field
	 *            ,日期改变的字段，YEAR,MONTH,DAY
	 * @param amount
	 *            ,改变量,增加的数量(减少用负数表示),如:5,-1
	 * @param strFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return
	 * @throws ParseException
	 */
	public static String getAddDate(Date date, String field, int amount,
			String strFormat) {
		// 当前日期和前一天

		SimpleDateFormat dateformat = new SimpleDateFormat(strFormat);
		Calendar rightNow = Calendar.getInstance();
		Date tempdate = date;
		rightNow.setTime(tempdate);

		int intField = 0;
		String tmpField = field.toUpperCase();

		intField = Calendar.DATE;
		if (tmpField.trim().equals(DEFAULT_YEAR))
			intField = Calendar.YEAR;
		else if (tmpField.trim().equals(DEFAULT_MONTH))
			intField = Calendar.MONTH;
		else if (tmpField.trim().equals(DEFAULT_DAY))
			intField = Calendar.DAY_OF_MONTH;
		else if (tmpField.trim().equals(DEFAULT_HOUR))
			intField = Calendar.HOUR_OF_DAY;
		else if (tmpField.trim().equals(DEFAULT_MINUTE))
			intField = Calendar.MINUTE;
		else if (tmpField.trim().equals(DEFAULT_SECOND))
			intField = Calendar.SECOND;

		rightNow.add(intField, amount);
		String day = dateformat.format(rightNow.getTime());
		return day;
	}

	/**
	 * string change to date yyyy-MM-dd HH:mm:ss
	 * 
	 * @param sDate
	 *            ,源日期字符串
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToTime(String sDate) throws ParseException {
		SimpleDateFormat dateformat = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
		if (sDate == null) {
			return null;
		}
		return dateformat.parse(sDate);
	}

	/**
	 * string change to date yyyy-MM-dd
	 * 
	 * @param sDate
	 *            ,源日期字符串
	 * @return Date,转为java.util.Date的日期
	 * @throws ParseException
	 */
	public static Date stringToDate(String sDate) throws ParseException {
		return stringToDate(sDate, DEFAULT_DATE_FORMAT);
	}

	/**
	 * string change to date yyyy-MM-dd
	 * 
	 * @param sDate
	 *            ,源日期字符串
	 * @param sFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return Date,转为java.util.Date的日期
	 * @throws ParseException
	 */
	public static Date stringToDate(String sDate, String sFormat)
			throws ParseException {
		SimpleDateFormat dateformat = new SimpleDateFormat(sFormat);
		if (sDate == null) {
			return null;
		}
		return dateformat.parse(sDate);
	}

	/**
	 * string change to date yyyy-MM-dd
	 * 
	 * @param sDate
	 *            ,源日期字符串
	 * @return Date,转为java.sql.Date的日期
	 * @throws ParseException
	 */
	public static java.sql.Date stringToSqlDate(String sDate)
			throws ParseException {
		return stringToSqlDate(sDate, DEFAULT_DATE_FORMAT);
	}

	/**
	 * string change to date yyyy-MM-dd
	 * 
	 * @param sDate
	 *            ,源日期字符串
	 * @param sFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return Date,转为java.sql.Date的日期
	 * @throws ParseException
	 */
	public static java.sql.Date stringToSqlDate(String sDate, String sFormat)
			throws ParseException {
		SimpleDateFormat dateformat = new SimpleDateFormat(sFormat);
		if (sDate == null) {
			return null;
		}
		return new java.sql.Date(dateformat.parse(sDate).getTime());
	}

	/**
	 * 将java.util.date型按照缺省格式转为字符串
	 * 
	 * @param date
	 *            ,源日期对象
	 * @return 如：21:10:12
	 */
	public static String timeToString(java.util.Date date) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat dateformat = new SimpleDateFormat(DEFAULT_TIME_FORMAT);
		String dateString = dateformat.format(date);
		return dateString;
	}

	/**
	 * 将java.util.date型按照指定格式转为字符串
	 * 
	 * @param d
	 *            ,源日期对象
	 * @param sFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return 如：2000-02-01 或2000-02-01 21:10:12
	 */
	public static String toString(java.util.Date date, String sFormat) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat dateformat = new SimpleDateFormat(sFormat);
		String dateString = dateformat.format(date);
		return dateString;
	}

	/**
	 * 将java.util.date型按照缺省格式转为字符串
	 * 
	 * @param date
	 *            ,源日期对象
	 * @return 如：2000-02-01
	 */
	public static String dateToString(java.util.Date date) {
		return toString(date, DEFAULT_DATE_FORMAT);
	}

	/**
	 * 将java.util.date型按照缺省格式转为字符串
	 * 
	 * @param date
	 *            ,源日期对象
	 * @return 如：2000-02-01
	 */
	public static String dateToString(java.util.Date date, String sFormat) {
		return toString(date, sFormat);
	}

	/**
	 * 将String型的日期按照指定格式转化
	 * 
	 * @param sDate
	 *            ,源日期对象
	 * @param sFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return 如：2000-02-01 或2000-02-01 21:10:12
	 */
	public static String dateToString(String sDate, String sFormat) {
		if (sDate == null) {
			return "";
		}
		java.sql.Timestamp ts = java.sql.Timestamp.valueOf(sDate);
		java.util.Date date = new java.util.Date(ts.getTime());
		return toString(date, sFormat);
	}

	/**
	 * Timestamp按照缺省格式转为字符串
	 * 
	 * @param ts
	 *            ,源日期对象
	 * @return 如：2000-02-01
	 */
	public static String dateToString(Timestamp ts) {
		return dateToString(ts, DEFAULT_DATE_FORMAT);
	}

	/**
	 * Timestamp按照指定格式转为字符串
	 * 
	 * @param ts
	 *            ,源日期对象
	 * @param sFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return 如：2000-02-01 或2000-02-01 21:10:12
	 */
	public static String dateToString(Timestamp ts, String sFormat) {
		if (ts == null) {
			return "";
		}
		java.util.Date date = new java.util.Date(ts.getTime());
		return toString(date, sFormat);
	}

	/**
	 * Timestamp按照缺省格式转为字符串，可指定是否使用长格式
	 * 
	 * @param ts
	 *            ,欲转化之变量Timestamp
	 * @param fullFormat
	 *            ,是否使用长格式
	 * @return 如：2000-02-01 或2000-02-01 21:10:12
	 */
	public static String dateToString(Timestamp ts, boolean fullFormat) {
		String s = null;
		if (fullFormat) {
			s = DEFAULT_DATETIME_FORMAT_SEC;
		} else {
			s = DEFAULT_DATE_FORMAT;
		}

		return dateToString(ts, s);
	}

	/**
	 * 将sqldate型按照缺省格式转为字符串
	 * 
	 * @param sqldate
	 *            ,源日期对象
	 * @return 如：2000-02-01
	 */
	public static String dateToString(java.sql.Date sqldate) {
		return toString(sqldate, DEFAULT_DATE_FORMAT);
	}

	/**
	 * 将sqldate型按照指定格式转为字符串
	 * 
	 * @param sqldate
	 *            ,源日期对象
	 * @param sFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return 如：2000-02-01 或2000-02-01 21:10:12
	 */
	public static String dateToString(java.sql.Date sqldate, String sFormat) {
		if (sqldate == null) {
			return "";
		}
		java.util.Date d = new java.util.Date(sqldate.getTime());
		return toString(d, sFormat);
	}

	/**
	 * 2009-11-25转换成2009年11月25日
	 * 
	 * @param s
	 * @return
	 * @throws ParseException
	 */
	public static String stringToChinesDate(String s) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat(DEFAULT_DATE_zh_CN_FORMAT);
		if (s == null) {
			return null;
		}
		return df.format(DateUtil.stringToDate(s));
	}

	/**
	 * 强制类型转换 从串到日期
	 * 
	 * @param sDate
	 *            ，源字符串，采用yyyy-MM-dd格式
	 * @param sFormat
	 *            ，目标字符串，如yyyy-MM-dd、yyyy-MM-dd
	 *            HH:mm:ss、yyyyMMddHHmmss、yyyy/MM/dd、yyyy/MM/dd HH:mm:ss等格式
	 * @return 得到的日期对象
	 * @throws ParseException
	 */
	public static String parseDate(String sDate, String sFormat) {
		String dateString;
		try {
			if (sDate == null)
				return null;
			DateFormat defaultDate = DateFormat.getDateInstance();
			java.util.Date date = defaultDate.parse(sDate);

			SimpleDateFormat dateformat = new SimpleDateFormat(sFormat);
			dateString = dateformat.format(date);

		} catch (Exception e) {
			dateString = today();
		}

		return dateString;
	}

	/**
	 * 强制类型转换 从串到时间戳
	 * 
	 * @param sDate
	 *            ，源串
	 * @param sFormat
	 *            ，遵循格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return 取得的时间戳对象
	 * @throws ParseException
	 */
	public static Timestamp parseTimestamp(String sDate, String sFormat)
			throws ParseException {
		SimpleDateFormat dateformat = null;

		java.util.Date utildate = null;
		dateformat = new SimpleDateFormat(sFormat);
		utildate = dateformat.parse(sDate);
		java.sql.Timestamp tsdate = new java.sql.Timestamp(utildate.getTime());
		return tsdate;
	}

	/**
	 * getCurDate 取当前日期
	 * 
	 * @return java.util.Date型日期
	 */
	public static java.util.Date getCurDate() {
		return (new java.util.Date());
	}

	/**
	 * getCurDate 取遵循格式的当前时间
	 * 
	 * @param sFormat
	 *            ，遵循格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return java.util.Date
	 */
	public static String getCurDate(String format) {
		SimpleDateFormat dateformat = null;
		try {
			dateformat = new SimpleDateFormat(format);
		} catch (Exception ex) {
			dateformat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		}
		java.util.Date date = new java.util.Date();
		String dateString = dateformat.format(date);
		return dateString;
	}

	/**
	 * 得到明天的时间
	 * 
	 * @param format
	 * @return
	 */
	public static String getTomorrowDate(String format) {
		SimpleDateFormat dateformat = null;
		try {
			dateformat = new SimpleDateFormat(format);
		} catch (Exception ex) {
			dateformat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		}
		java.util.Date date = new java.util.Date();
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime();
		String dateString = dateformat.format(date);
		return dateString;
	}

	/**
	 * getCurTimestamp 取当前时间戳
	 * 
	 * @return java.sql.Timestamp
	 */
	public static java.sql.Timestamp getCurTimestamp() {
		java.util.Date today = new java.util.Date();
		java.sql.Timestamp ts = new java.sql.Timestamp(today.getTime());
		return ts;
	}

	/**
	 * 获取年份
	 * 
	 * @param date
	 * @return int
	 */
	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int year = cal.get(Calendar.YEAR);
		return year;
	}

	/**
	 * 获取月份
	 * 
	 * @param date
	 * @return int
	 */
	public static int getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int month = cal.get(Calendar.MONTH);
		return month + 1;
	}

	/**
	 * 获取周数
	 * 
	 * @param date
	 * @return int
	 */
	public static int getWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int week = cal.get(Calendar.WEEK_OF_YEAR);
		return week;
	}

	/**
	 * 获取当月第一天
	 * 
	 * @return Date
	 */
	public static Date getFirstDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 1);// 设为当前月的1号
		Date newDate = cal.getTime();
		return newDate;
	}

	/**
	 * 计算当月最后一天
	 * 
	 * @return Date
	 */
	public static Date getLastDayOfMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 1);// 设为当前月的1号
		cal.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		cal.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
		Date newDate = cal.getTime();
		return newDate;
	}

	public static int getLastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.DATE, -1);
		return cal.get(Calendar.DATE);
	}

	/**
	 * 获取当月第一天
	 * 
	 * @return Date
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);// 设为当前月的1号
		Date newDate = cal.getTime();
		return newDate;
	}

	/**
	 * 计算当月最后一天
	 * 
	 * @return Date
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);// 设为当前月的1号
		cal.add(Calendar.MONTH, 1);// 加一个月，变为下月的1号
		cal.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
		Date newDate = cal.getTime();
		return newDate;
	}

	/**
	 * 计算两个日期的时间差（秒）
	 * 
	 * @param bTime
	 * @param eTime
	 * @return
	 * @throws ParseException
	 */
	public static String spanTime(String bTime, String eTime) {
		SimpleDateFormat dateformat = new SimpleDateFormat(
				DEFAULT_DATETIME_FORMAT_SEC);
		String result = "";
		if (bTime == null || bTime.trim().equals(""))
			return "0 秒";

		if (eTime == null || eTime.trim().equals(""))
			return "0 秒";

		try {
			Date beginTime = dateformat.parse(bTime);
			Date endTime = dateformat.parse(eTime);
			long st = endTime.getTime() - beginTime.getTime() > 0 ? endTime
					.getTime() - beginTime.getTime() : beginTime.getTime()
					- endTime.getTime();
			result = st / 1000 + " 秒";
		} catch (ParseException e) {
			result = "0 秒";
		}

		return result;
	}

	/**
	 * 字符串转时间 格式
	 * 
	 * @param strDate
	 *            日期
	 * @param format
	 *            "yyyy-MM-dd"
	 * @return
	 */
	public static Date getDateParse(String strDate, String format) {
		return new SimpleDateFormat(format)
				.parse(strDate, new ParsePosition(0));
	}

	public static String getTime(Date start, Date end) {

		StringBuffer stringBuffer = new StringBuffer();
		long timeMillis = end.getTime() - start.getTime();
		long m = 0;
		int day = 0;
		int hour = 0;
		int min = 0;
		int s = 0;
		day = (int) (timeMillis / (1000 * 60 * 60 * 24));
		m = timeMillis % (1000 * 60 * 60 * 24);
		hour = (int) (m / (1000 * 60 * 60));
		m = m % (1000 * 60 * 60);
		min = (int) (m / (1000 * 60));
		m = m % (1000 * 60);
		s = (int) (m / (1000));
		if (day > 0) {
			stringBuffer.append(getDateTime(start));
		} else if (hour >= 1) {
			String sameday1 = dateToString(start);
			String sameday2 = dateToString(end);
			if (sameday1.equals(sameday2)) {
				stringBuffer.append("今天 " + toString(start, "HH:mm"));
			} else {
				stringBuffer.append(getDateTime(start));
			}
			// stringBuffer.append(hour+"小时前");
		} else if (min > 0) {
			stringBuffer.append(min + "分钟前");
		} else if (s > 10) {
			stringBuffer.append(s + "秒前");
		} else {
			stringBuffer.append("10秒前");
		}
		return stringBuffer.toString();
	}

	public static String getDateTime(Date date) {
		return getMonthDay(date) + " " + toString(date, "HH:mm");
	}

	/**
	 * @Title: getMonthDay
	 * @param date
	 * @return String 12月5日
	 * @throws
	 */
	public static String getMonthDay(Date date) {

		StringBuffer stringBuffer = new StringBuffer();
		Calendar calendar = Calendar.getInstance();
		if (null != date) {
			calendar.setTime(date);
			stringBuffer
					.append(String.valueOf(calendar.get(Calendar.MONTH) + 1)
							+ "月");
			stringBuffer.append(String.valueOf(calendar
					.get(Calendar.DAY_OF_MONTH)) + "日");
		}
		return stringBuffer.toString();
	}

	/**
	 * 功能：对于给定的时间增加天数/月数/年数后的日期,按指定格式输出
	 * 
	 * @param date
	 *            ,要改变的日期
	 * @param field
	 *            ,日期改变的字段，YEAR,MONTH,DAY
	 * @param amount
	 *            ,改变量,增加的数量(减少用负数表示),如:5,-1
	 * @param strFormat
	 *            ,日期返回格式，如"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"
	 * @return
	 * @throws ParseException
	 */
	public static Date getAddDateReturnDate(Date date, String field,
			int amount, String strFormat) {

		Calendar rightNow = Calendar.getInstance();
		Date tempdate = date;
		rightNow.setTime(tempdate);

		int intField = 0;
		String tmpField = field.toUpperCase();

		intField = Calendar.DATE;
		if (tmpField.trim().equals(DEFAULT_YEAR))
			intField = Calendar.YEAR;
		else if (tmpField.trim().equals(DEFAULT_MONTH))
			intField = Calendar.MONTH;
		else if (tmpField.trim().equals(DEFAULT_DAY))
			intField = Calendar.DAY_OF_MONTH;
		else if (tmpField.trim().equals(DEFAULT_HOUR))
			intField = Calendar.HOUR_OF_DAY;
		else if (tmpField.trim().equals(DEFAULT_MINUTE))
			intField = Calendar.MINUTE;
		else if (tmpField.trim().equals(DEFAULT_SECOND))
			intField = Calendar.SECOND;

		rightNow.add(intField, amount);
		return rightNow.getTime();
	}

	public static void main(String[] args) {
		try {
			// System.out.println(DateUtil.dateToString("2010-1-12 8:23:33.900",
			// DateUtil.DEFAULT_DATETIME_FORMAT_SEC));
			System.out.println(dateToString(getLastDayOfMonth(),
					DateUtil.DEFAULT_DATE_FORMAT));
			System.out.println(dateToString(
					getLastDayOfMonth(stringToDate("2010-02-11")),
					DateUtil.DEFAULT_DATE_FORMAT));
			System.out.println(getWeek(stringToDate("2010-01-11",
					DateUtil.DEFAULT_DATE_FORMAT)));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 根据年月返回月的最后一天
	public static int getDayByYearAndMonth(int year, int month) {
		// 获取某月的天数
		Calendar time = Calendar.getInstance();
		time.clear();
		time.set(Calendar.YEAR, year);
		time.set(Calendar.MONTH, month - 1);// 注意,Calendar对象默认一月为0
		int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
		return day;
	}

}
